import Vue from 'vue'
import Loading from '@/components/common/Loading.vue'
import store from '@/store'

let loadingInstance = null

/**
 * 显示全局加载
 * @param {Array} texts 可选的加载文本数组
 */
export const showLoading = (texts) => {
  // 检查是否有开屏动画正在显示
  const splashScreenVisible = document.querySelector('.splash-screen') && 
    !document.querySelector('.splash-screen.fade-out');
    
  // 如果有开屏动画正在显示，则不显示加载
  if (splashScreenVisible) {
    return;
  }
  
  // 设置加载文本（如果提供）
  if (texts && Array.isArray(texts) && texts.length > 0) {
    store.commit('SET_LOADING_TEXTS', texts);
  }
  
  if (!loadingInstance) {
    const LoadingConstructor = Vue.extend(Loading)
    loadingInstance = new LoadingConstructor()
    loadingInstance.$mount()
    document.body.appendChild(loadingInstance.$el)
  }
  
  loadingInstance.visible = true
  store.commit('SET_LOADING', true);
}

/**
 * 隐藏全局加载
 */
export const hideLoading = () => {
  if (loadingInstance) {
    loadingInstance.visible = false
  }
  store.commit('SET_LOADING', false);
} 